/**
 * @author ZCR
 * @create 2025/8/16 21:26
 */
public class Solution151 {
    public String reverseWords(String s) {
        s = s.trim();
        StringBuilder clearStr=new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (i>0 &&s.charAt(i) == ' '&&s.charAt(i-1) == ' ') {
                continue;
            }else{
                clearStr.append(s.charAt(i));
            }
        }
        String [] splits = clearStr.toString().split(" ");
        StringBuilder result= new StringBuilder();
        for(int i=splits.length-1;i>=0;i--){
            result.append(splits[i]).append(" ");
        }
        return result.toString().trim();
    }

    public static void main(String[] args) {
        Solution151 solution = new Solution151();
        System.out.println(solution.reverseWords("the sky is blue"));
        System.out.println(solution.reverseWords("  hello world  "));
        System.out.println(solution.reverseWords("a good   example"));
    }
}
